package com.zhongzhi.javafx.controller;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.ResourceBundle;
import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;

import com.zhongzhi.common.Common;
import com.zhongzhi.common.Plc;
import com.zhongzhi.common.Primary;
import com.zhongzhi.springboot.pojo.Equipment;
import com.zhongzhi.springboot.pojo.EquipmentShow;

import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;

public class TABSController implements Initializable{
	@FXML
	private TabPane TP;

	/**
	 * 通过公共对象,获取本机所管理的plc
	 * 根据plc数量,建立tab数量的页面
	 */
	@Override
	public void initialize(URL location, ResourceBundle resources) {
		
		Ref();
						
	}
	private void Ref() {
		// TODO Auto-generated method stub
		if (TP.getTabs().size()==0) {
			HashMap<Integer, Plc> map = Primary.getMap();
			
			if (map!=null) {
				Set<Integer> keySet = map.keySet();
				int a =1;
				for (Integer integer : keySet) {
					try {
						Plc plc = map.get(integer);
						Tab tab= new Tab();
						tab = FXMLLoader.load(getClass().getResource("/fxml/mod/TAB.fxml"));
						tab.setUserData(plc);
						tab.setText(plc.getPlcName()+": "+plc.getHost());
						TP.getTabs().add(tab);
						if (a==Common.getPage()) {
							TP.getSelectionModel().select(tab);
						}
						a++;
					} catch (IOException e) {
						
						e.printStackTrace();
					}
				}
				
				
			}
			
		}
	}

}
